// pages/video/details/details.js

//const app = getApp()
const app = getApp()
var bookname,bookid,bookphotourl,bookprice,bookdiscount,sellerid
var sleep = function(time) {
  var startTime = new Date().getTime() + parseInt(time, 10);
  while(new Date().getTime() < startTime) {}
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    console.log("id:",option.id)
    // 调用云函数
    wx.cloud.callFunction({
      name: 'get_information',
      data: { id: option.id, databasename:'book' },
      success: res => {
        let { data } = res.result
        console.log(res.result)
        console.log('[云函数] [video] user', data[0])
        let { _id,name, author, price, grade, discount, sales, url, seller_id } = data[0]
        bookid=_id,bookname=name,bookphotourl=url,bookprice=price,bookdiscount=discount,sellerid=seller_id
        console.log(sellerid)
        this.setData({
          name: name,
          author: author,
          price: price,
          grade:grade,
          discount:discount,
          sales:sales,
          url: url
        })
      },
      fail: err => {
        console.error('[云函数] [video] 调用失败', err)
      }

    })
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    this.setData({
      disabled:false
    })
  },
  //跳转到购物车
  toShoppincart: function () {
    wx.switchTab({ url: '../shoppingcart/shoppingcart' })
  },
  //加入购物车
  addTocart: function () {
    this.setData({
      disabled:true
    })
    const db = wx.cloud.database()
    const userid = app.globalData.openid
    console.log(userid)
    db.collection('shoppingcart').where({
      bookid:bookid,
      _openid:userid
    }).get({
      success: function(res){
        console.log(res.data)
        //成功添加
        if(res.data.length==0){
          db.collection('shoppingcart').add({
            data: {
              //userid: app.globalUserid,
              bookname:bookname,
              bookprice:bookprice,
              bookphotourl:bookphotourl,
              bookid:bookid,
              bookdiscount:bookdiscount,
              seller_id: sellerid
            },
            success: function(res) {
              wx.showToast({
                title: '添加成功',
                icon: 'succes',
                duration: 1000,
              })
              
            }
          })
        }
        //商品已在购物车
        else{
          wx.showToast({
            title: '已经在购物车',
            icon:'none',
            duration: 1000,
          })
        }
        
      },
      fail: function(){
        
      }
    })
    sleep(500)
    this.onShow()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})